<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
$module			= $_GET['module'];
$action			= $_GET['action'];
$field_name		= $_GET['field_name'];
$field_type		= $_GET['field_type'];
$table_id		= $_GET['table_id'];
$field_id		= $_GET['field_id'];
$is_array		= $_GET['is_array'];

$project_home 	= $_GET['project_home'];
$SEMF_ROOT		= '../../';
$module_xml = $SEMF_ROOT.$project_home.$module.'/module.xml';

$doc = new DOMDocument();
$root = NULL;

//Assign relevant table  to root
if (file_exists($module_xml)) {
	$doc->load($module_xml);
	$relevant_table = $doc-> getElementsByTagName('table')-> item($table_id);
}
else
{
	$root = $doc->createElement('module');
	$doc-> appendChild($root);
	$relevant_table = $doc->createElement('table');
	$root->appendChild($relevant_table);
}

if($relevant_table->hasChildNodes()){
	$field_list = $relevant_table-> childNodes;
}
if($action == 'new'){
		$new_field = $doc->createElement('field');
		$new_field-> setAttribute('name', $field_name);
		$new_field-> setAttribute('type', $field_type);
		if($is_array == 'yes')
		{
			$new_field-> setAttribute('isarray',$is_array);
		}
		$relevant_table-> appendChild($new_field);
		echo $relevant_table->getAttribute('name');
}
else if (  $action == 'edit' )
{
		$field_list-> item($field_id)-> setAttribute('name', $field_name);
		$field_list-> item($field_id)-> setAttribute('type', $field_type);
		if($is_array == 'yes')
		{
			$field_list-> item($field_id)->setAttribute('isarray',$is_array);
		}else {
			$field_list-> item($field_id)->removeAttribute('isarray');
		}
		echo $relevant_table->getAttribute('name');
}
else if( $action == 'delete')
{
	if($field_list->length > $field_id)
	{
		$relevant_table-> removeChild( $field_list-> item($field_id) );
	}
	echo $relevant_table->getAttribute('name');

}

$doc->save($module_xml);
?>